package com.example.app.manager

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.text.style.ForegroundColorSpan
import android.view.LayoutInflater
import android.view.View
import android.view.animation.LinearInterpolator
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.ViewUtils
import com.example.app.R
import com.libcommon.GlideManager
import com.libcommon.Spanny
import com.libcommon.ext.getNickName

/**
 * CreateBy:Joker
 * CreateTime:2024/9/11 20:03
 * description：banner移动播放器
 */
class DanMuPlayer {

    private var animatorSet: AnimatorSet? = null

    private val listChannel = floatArrayOf(0f, 55f, 90f, 125f, 160f, 195f)

    @SuppressLint("InflateParams", "SetTextI18n")
    fun startPlay(viewRoot: FrameLayout, avatar: String?, name: String?, vipLevel: Int?, text: String, playResult: () -> Unit) {

        val danMuView = LayoutInflater.from(viewRoot.context).inflate(R.layout.layout_dan_mu, null, false)
        val ivAvatar = danMuView.findViewById<ImageView>(R.id.ivAvatar)
        val tvContent = danMuView.findViewById<TextView>(R.id.tvContent)

        ivAvatar.updateLayoutParams<ConstraintLayout.LayoutParams> { topMargin = ConvertUtils.dp2px((listChannel[(0..5).random()])) }
        GlideManager.loadAvatarByVip(danMuView.context, avatar, ivAvatar, vipLevel, true)

        val nickname = name?.getNickName()
        val span = Spanny(if (ViewUtils.isLayoutRtl()) "$text:$nickname" else "$nickname:${text}")
        span.findAndSpan(if (ViewUtils.isLayoutRtl()) ":$nickname" else "$nickname:") { ForegroundColorSpan(ColorUtils.getColor(R.color.color_black)) }
        tvContent.text = span

        viewRoot.addView(danMuView)

        val sw = ScreenUtils.getScreenWidth().toFloat()
        val startX = if (ViewUtils.isLayoutRtl()) -sw else sw
        val endX = -startX

        danMuView.translationX = startX //初始位置，屏幕右侧
        danMuView.setLayerType(View.LAYER_TYPE_HARDWARE, null)

        val animators = listOf<ObjectAnimator>(
            //1、bannerView显示出来
            ObjectAnimator.ofFloat(danMuView, View.ALPHA, 0f, 1f).apply {
                duration = 1000L
                interpolator = LinearInterpolator()
            },

            //2、需要移动的view 移动进来
            ObjectAnimator.ofFloat(danMuView, View.TRANSLATION_X, startX, endX).apply {
                duration = 5000L
                interpolator = LinearInterpolator()
            },

            //3、bannerView消失
            ObjectAnimator.ofFloat(danMuView, View.ALPHA, 1f, 0f).apply {
                duration = 1000L
                interpolator = LinearInterpolator()
            }
        )

        animatorSet = AnimatorSet().apply {
            playSequentially(animators)
            start()
            addListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator) {
                    super.onAnimationEnd(animation)
                    viewRoot.removeView(danMuView)
                    playResult.invoke()
                }
            })
        }
    }

    fun stopPlay() {
        animatorSet?.apply {
            removeAllListeners()
            cancel()
        }
        animatorSet = null
    }

    fun isPlaying(): Boolean {
        return animatorSet?.isRunning == true
    }
}